@extends('backend.layouts.app')

@section('content')
<div class="layui-card">
    <div class="layui-card-header layui-card-header-auto">
        专业列表
    </div>
    <div class="layui-card-body">
        <div class="layui-row" style="margin-bottom:10px">
            <div class="layui-col-md9">
                <div class="layui-inline">
                    <input type="text" id="key" placeholder="请输入专业名称/编号" class="layui-input">
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layui-btn-sm" onclick="search()">搜索</button>
                </div>
            </div>
            <div class="layui-col-md3">
                <div class="layui-btn-group" style="float:right">
                    <a href="{{route('admin.base.subject.create')}}" class="layui-btn layui-btn-sm">添加</a>
                </div>
            </div>
        </div>
        <table id="dataTable" lay-filter="dataTable">
            <script type="text/html" id="options">
                <div class="layui-btn-group">
                    <a class="layui-btn layui-btn-sm" lay-event="show">查看</a>
                    <a class="layui-btn layui-btn-sm" lay-event="edit">编辑</a>
                    <a class="layui-btn layui-btn-sm layui-btn-danger" lay-event="del">删除</a>
                </div>

            </script>
        </table>
    </div>
</div>
@endsection

@section('script')
<script type="text/javascript">
    let dataTable;
    layui.use(['layer', 'form', 'table'], function () {
        let layer = layui.layer;
        let form = layui.form;
        let table = layui.table;

        dataTable = table.render({
            elem: '#dataTable',
            url: "{{route('admin.base.subject.pageList')}}",
            page: true,
            cols: [
                [{
                    type: 'numbers',
                    fixed: true
                }, {
                    title: '名称',
                    field: 'name'
                }, {
                    title: '专业代码',
                    field: 'code'
                }, {
                    title: '类别',
                    field: 'category'
                }, {
                    title: '备注',
                    field: 'description'
                }, {
                    fixed: 'right',
                    align: 'center',
                    toolbar: '#options'
                }]
            ]
        });

        table.on('tool(dataTable)', function (obj) {
            let data = obj.data;
            let layEvent = obj.event;
            switch (layEvent) {
                case 'show':
                    location.href = data.subject_id + '/show';
                    break;
                case 'edit':
                    location.href = data.subject_id + '/edit';
                    break;
                case 'del':
                    layer.confirm('确认删除吗？', function (index) {
                        $.post("{{ route('admin.base.subject.destroy') }}", {
                                '_method': 'delete',
                                '_token': '{{csrf_token()}}',
                                ids: data.role_id
                            },
                            function (result) {
                                if (result.code == 0) {
                                    obj.del(); //删除对应行（tr）的DOM结构
                                }
                                layer.close(index);
                                layer.msg(result.msg);
                            });
                    });
                    break;
                default:
                    break;
            }
        });
    });

    function search() {
        dataTable.reload({
            where: {
                key: $("#key").val()
            },
            page: {
                curr: 1
            }
        });
    }

</script>
@endsection
